package com.docjohnson.pamphlet.activities;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.docjohnson.pamphlet.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Download extends Activity {
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

    private ProgressDialog mProgressDialog;
    public String app_name;
    public String urlpath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
        app_name = "test1.zip";
        urlpath = "http://www.docjohnson.com/test_dev/media/Pamphlet/download/data.zip";
        if (android.os.Environment.getExternalStorageState().equals
                (android.os.Environment.MEDIA_MOUNTED)) {
            startDownload();
        } else {
            Toast.makeText(getApplicationContext(), "SD Card not found,Insert SD card and try again", Toast.LENGTH_SHORT).show();
        }
    }

    private void startDownload() {
        new DownloadFileAsync().execute(urlpath);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
//        switch (id) {
                return null;
//        }
    }

    class DownloadFileAsync extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected String doInBackground(String... aurl) {
            try {
                URL url = new URL(urlpath.toString()); // Your given URL.

                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect(); // Connection Complete here.!

                int lenghtOfFile = c.getContentLength();

                Log.d("Downloading Updates", "Lenght of file: " + lenghtOfFile);

                String PATH = Environment.getExternalStorageDirectory() + "/download/";

                File file = new File(PATH); // PATH = /mnt/sdcard/download/

                if (!file.exists()) {
                    file.mkdirs();
                }
                File outputFile = new File(file, app_name);
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                long total = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    total += len1;
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                    fos.write(buffer, 0, len1); // Write In FileOutputStream.
                }
                fos.flush();
                fos.close();
                is.close();
            } catch (Exception e) {
            }
            return null;

        }

        protected void onProgressUpdate(String... progress) {
            Log.d("Downloading Updates", progress[0]);
            mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//            finish();
        }
    }
}